﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Threading;


namespace Daphne.Forms
{
    /// <summary>
    /// Fatal error dialog code-behind class
    /// </summary>
    public partial class FatalErrorDialog : Form
    {
        private const string _sSoftwareURL = "http://daphne.codeplex.com";


        public FatalErrorDialog()
        {
            this.StartPosition = FormStartPosition.CenterScreen;
            InitializeComponent();

            btnClose.Click += btnClose_Click;
            btnCopyToClipboard.Click += btnCopyToClipboard_Click;
            lnkURL.LinkClicked += lnkURL_LinkClicked;
        }

        public void SetCulture(string culture)
        {
            CultureInfo ci = new CultureInfo(culture);
            Thread.CurrentThread.CurrentCulture = ci;
            Thread.CurrentThread.CurrentUICulture = ci;
        }

        /// <summary>
        /// Navigates to the software web site
        /// </summary>
        void lnkURL_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start(new ProcessStartInfo(_sSoftwareURL));
        }

        void btnCopyToClipboard_Click(object sender, EventArgs e)
        {
            Clipboard.Clear();
            Clipboard.SetText(txtDebugOutput.Text);

            ComponentResourceManager resource = new ComponentResourceManager(typeof(FatalErrorDialog));
            MessageBox.Show(resource.GetString("ErrorCopied"));
        }

        /// <summary>
        /// Closes the dialog
        /// </summary>
        void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
        /// <summary>
        /// Sets the exception to the debug output window
        /// </summary>
        public void SetException(Exception ex)
        {
            StringBuilder sb = new StringBuilder();
            Exception inner = ex;
            while (inner != null)
            {
                sb.AppendLine(inner.Message);
                sb.AppendLine(inner.StackTrace);
                inner = inner.InnerException;
            }

            txtDebugOutput.Text = sb.ToString();
        }


    }
}
